package com.jzr.medical.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzr.common.util.U;
import com.jzr.medical.common.LayServiceImpl;
import com.jzr.medical.db1.dto.WorkPlanDto;
import com.jzr.medical.db1.mapper.TWorkPlanMapper;
import com.jzr.medical.db1.mapper.VBedMapper;
import com.jzr.medical.db1.model.TWorkPlan;
import com.jzr.medical.db1.service.WorkPlanService;
import com.jzr.medical.db1.vo.WorkPlanBedVo;
import com.jzr.medical.db1.vo.WorkPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkPlanServiceImpl extends LayServiceImpl<TWorkPlanMapper,TWorkPlan,WorkPlanVo,Integer> implements WorkPlanService {
    @Autowired
    VBedMapper vBedMapper;

    @Override
    public IPage<WorkPlanVo> page(WorkPlanDto record) {
        IPage<WorkPlanVo> page = new Page<>(record.getPageNum(),record.getPageSize());

        QueryWrapper<WorkPlanVo> qw = new QueryWrapper<>();
        qw.orderByAsc(record.getOrderByClause());
        qw.eq(U.isNotBlank(record.getDeptCode()),WorkPlanVo.DeptCode_,record.getDeptCode())
                .eq(U.isNotBlank(record.getWorkType()),WorkPlanVo.WorkType_,record.getWorkType())
                .ge(U.isNotBlank(record.getPlanDateFrom()),WorkPlanVo.PlanDate_,record.getPlanDateFrom())
                ;
        if(U.isNotBlank(record.getPlanDateTo())){
            qw.lt(WorkPlanVo.PlanDate_,record.getPlanDateTo().plusDays(1));
        }
        List<WorkPlanVo> list = this.list(page,qw);

        if(list == null || list.size()<=0){
            return null;
        }
        List<WorkPlanBedVo> bedAlls = baseMapper.getBedList(list.get(0).getDeptCode());
        if(bedAlls != null && bedAlls.size()>0) {
            Map<String, Object> bedMap = new HashMap<>();
            for (WorkPlanBedVo w : bedAlls) {
                bedMap.put(w.getBedId().toString(), w);
            }
            for (WorkPlanVo w : list) {
                String beds = w.getBeds();
                if (beds != null && beds.length() > 0) {
                    String[] bedArray = beds.split(",");
                    List<WorkPlanBedVo> bedList = new ArrayList<>();
                    for (String id : bedArray) {
                        WorkPlanBedVo bedVo = (WorkPlanBedVo) bedMap.get(id);
                        if (bedVo != null) {
                            bedList.add(bedVo);
                        }
                    }
                    w.setBedList(bedList);
                    w.setBeds(null);
                }
            }
        }
        page.setRecords(list);
        return page;
    }

    private void setBeds(WorkPlanVo record){
        List<WorkPlanBedVo> list = record.getBedList();
        if(list != null && list.size()>0){
            StringBuilder beds = new StringBuilder(20);
            for(WorkPlanBedVo bedVo : list){
                if(beds.length()>0){
                    beds.append(",");
                }
                beds.append(bedVo.getBedId());
            }
            record.setBeds(beds.toString());
        }
    }

    @Override
    public WorkPlanVo saveAndGet(WorkPlanVo record) {
        this.setBeds(record);
        setDefaultValue(record);
        baseMapper.insert(record);
        return baseMapper.getById(record.getId());
    }

    @Override
    public WorkPlanVo updateAndGet(WorkPlanVo record) {
        if(record.getId() == null){
            return saveAndGet(record);
        }
        this.setBeds(record);
        setDefaultValue(record);
        baseMapper.updateById(record);

        return baseMapper.getById(record.getId());
    }


}
